package com.tencent.compose.map.poi.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.launch
import org.example.project.search.data.Suggestion
import org.example.project.search.ui.FooterRecordItem
import org.example.project.search.ui.Img
import org.example.project.search.ui.RecordItem

/**
 * 搜索记录卡片
 */
@Composable
fun SearchRecordCard(list: List<Suggestion>, modifier: Modifier = Modifier) {
    if (list.isNotEmpty()) {
        if (list[0].suggestionsInCity.isNullOrEmpty()) {
            LazyColumn(
                modifier = Modifier.fillMaxSize().padding(bottom = 10.dp),
            ) {
                item {
                    Column(
                        modifier
                            .fillMaxWidth()
                            .padding(top = 8.dp)
                            .background(
                                color = Color.White, shape = RoundedCornerShape(16.dp)
                            )
                            .padding(start = 12.dp, top = 8.dp, end = 12.dp, bottom = 8.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        list.forEachIndexed { index, it ->
                            if (it.type == Suggestion.TYPE_FOOTER.toLong()) {
                                Column(
                                    modifier = modifier
                                        .fillMaxWidth()
                                        .clickable { }
                                        .padding(top = 5.dp, end = 5.dp),
                                    horizontalAlignment = Alignment.CenterHorizontally
                                ) {
                                    Text(
                                        it.footerText ?: "查看全部结果",
                                        fontSize = 11.sp,
                                        fontWeight = FontWeight.W400,
                                        color = Color(0xdd000000),
                                    )
                                }
                            } else {
                                RecordItem(it)
                            }
                            if (index != (list.size - 1)) {
                                Divider(
                                    modifier = Modifier.padding(top = 4.dp, bottom = 4.dp),
                                    thickness = 0.5.dp
                                )
                            }
                        }

                    }
                    Spacer(Modifier.height(400.dp))
                }
            }
        } else {
            val listState = rememberLazyListState()
            val currentCity by derivedStateOf {
                list[listState.firstVisibleItemIndex].shortCityName
            }
            val coroutineScope = rememberCoroutineScope()

            // 顶部分栏
            LazyRow(
                Modifier.fillMaxWidth()
                    .padding(start = 8.dp, end = 8.dp, top = 6.dp, bottom = 6.dp)
            ) {
                itemsIndexed(list) { index, it ->
                    if (it.type != Suggestion.TYPE_FOOTER.toLong()) {
                        Column(
                            modifier = Modifier
                                .padding(end = 25.dp)
                                .clickable {
                                    coroutineScope.launch {
                                        listState.scrollToItem(index)
                                    }
                                },
                            horizontalAlignment = Alignment.CenterHorizontally
                        ) {
                            Text(
                                it.shortCityName ?: "",
                                fontSize = 16.sp,
                                fontWeight = FontWeight.W400
                            )
                            if (currentCity == it.shortCityName) {
                                Box(
                                    modifier = Modifier
                                        .padding(top = 5.dp)
                                        .size(width = 25.dp, 5.dp)
                                        .background(
                                            Color(0xff3f88f5),
                                            shape = RoundedCornerShape(15.dp)
                                        )
                                )
                            }
                        }
                    }
                }
            }

            // 搜索卡片
            LazyColumn(
                modifier = Modifier.fillMaxSize().padding(bottom = 10.dp),
                state = listState
            ) {
                itemsIndexed(list) { index, sugItem ->
                    if (sugItem.type == Suggestion.TYPE_FOOTER.toLong()) {
                        FooterRecordItem(sugItem)
                    } else {
                        Column(
                            modifier = Modifier.fillMaxWidth()
                                .padding(top = 8.dp)
                                .background(
                                    color = Color.White, shape = RoundedCornerShape(
                                        topStart = if (index == 0) 16.dp else 0.dp,
                                        topEnd = if (index == 0) 16.dp else 0.dp,
                                        bottomStart = if (index == list.size - 1) 16.dp else 0.dp,
                                        bottomEnd = if (index == list.size - 1) 16.dp else 0.dp
                                    )
                                )
                                .padding(start = 12.dp, top = 8.dp, end = 12.dp, bottom = 8.dp)
                        ) {
                            Row(
                                modifier = Modifier.fillMaxWidth().padding(end = 10.dp),
                                verticalAlignment = Alignment.CenterVertically,
                                horizontalArrangement = Arrangement.SpaceBetween
                            ) {
                                Text(
                                    sugItem.cityName ?: "",
                                    fontSize = 16.sp,
                                    fontWeight = FontWeight.W400
                                )
                                Img(14.dp)
                            }
                            Spacer(Modifier.height(8.dp))
                            if (!sugItem.suggestionsInCity.isNullOrEmpty()) {
                                sugItem.suggestionsInCity!!.forEachIndexed { index, it ->
                                    RecordItem(it)
                                    if (index < sugItem.suggestionsInCity!!.size - 1) {
                                        Divider(
                                            modifier = Modifier.padding(top = 4.dp, bottom = 4.dp),
                                            thickness = 0.5.dp
                                        )
                                    }
                                }
                            }
                        }
                    }
                }

                item {
                    // 底部
                    Spacer(Modifier.height(600.dp))
                }
            }
        }
    }
}
